package com.school.serviceImpl;

import com.school.dao.ScoreMapper;
import com.school.dto.score.GetScoreDTO;
import com.school.model.Score;
import com.school.model.User;
import com.school.service.DepartmentService;
import com.school.service.ScoreService;
import com.school.service.UserService;
import com.school.vo.score.ScoreAllRankAndUserNameVO;
import com.school.vo.score.ScoreAllRankVO;
import com.school.vo.score.ScoreRankVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired(required = false)
    private ScoreMapper scoreMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;


    @Override
    public synchronized int insertSelective(Score record) {
        if (scoreMapper.selectByTidAndCommentatorId(record.gettId(), record.getCommentatorId()) != null) {
            return -1;//已做过评论
        }
        User tUser = userService.selectByPrimaryKey(record.gettId());
        User commentatorUser = userService.selectByPrimaryKey(record.getCommentatorId());
        if (tUser == null || commentatorUser == null || tUser.getRole() != 2) {
            return -2;//教师编号或评论者编号错误
        }
        if (tUser.getDepartmentId().intValue() != commentatorUser.getDepartmentId().intValue()) {
            if (commentatorUser.getRole() == 2) {
                return -3;//与教师只允许评论自己系的教师
            }
        }


        if(commentatorUser.getRole() == 1){
            String teachers=commentatorUser.getClassTeacherId();
            List<String> teacherId=new ArrayList<>();
            if(teachers.indexOf(".")==-1){
                teacherId.add(teachers);
            }else{
                String right=teachers.substring(teachers.indexOf(".")+1,teachers.length());
                String left=right.substring(0,right.lastIndexOf("."));
                String [] ids=left.split("\\.\\.");
                for(int i =0;i<ids.length;i++){
                    teacherId.add(ids[i]);
                }
            }
            if(!teacherId.contains(tUser.getId()+"")){
                return -4;//学生只能评论自己的任课教师。
            }

        }
        record.setCreateTime(new Date());
        record.setCommentatorRole(commentatorUser.getRole());
        return scoreMapper.insertSelective(record);
    }

    public List<Score> selectAVGBytTId(Integer tId) {
        return scoreMapper.selectAVGBytTId(tId);
    }

    @Override
    public Score getByTIdAndDId(GetScoreDTO dto) {
        return scoreMapper.getByTIdAndDId(dto);
    }

    @Override
    public ScoreAllRankVO getScoreAndNumberByTId(Integer tId) {
        ScoreAllRankVO vo=new ScoreAllRankVO();
        User user=userService.selectByPrimaryKey(tId);
        List<ScoreRankVO> allSumRank =scoreMapper.sumRank(tId,null,null,null,null);
        vo.setAllSumRank(allSumRank.size()==0?null:allSumRank.get(0).getRank());
        vo.setAllSumRankScore(allSumRank.size()==0?null:allSumRank.get(0).getNumber());

        List<ScoreRankVO> allAvgRank =scoreMapper.avgRank(tId,null,null,null,null);
        vo.setAllAvgRank(allAvgRank.size()==0?null:allAvgRank.get(0).getRank());
        vo.setAllAvgRankScore(allAvgRank.size()==0?null:allAvgRank.get(0).getNumber());

        List<ScoreRankVO> allCountRank =scoreMapper.countRank(tId,null,null,null,null);
        vo.setAllCountRank(allCountRank.size()==0?null:allCountRank.get(0).getRank());
        vo.setAllCountRankScore (allCountRank.size()==0?null:allCountRank.get(0).getNumber());

        List<ScoreRankVO> depSumRank =scoreMapper.sumRank(tId,user.getDepartmentId(),null,null,null);
        vo.setDepSumRank(depSumRank.size()==0?null:depSumRank.get(0).getRank());
        vo.setDepSumRankScore(depSumRank.size()==0?null:depSumRank.get(0).getNumber());

        List<ScoreRankVO> depAvgRank =scoreMapper.avgRank(tId,user.getDepartmentId(),null,null,null);
        vo.setDepAvgRank(depAvgRank.size()==0?null:depAvgRank.get(0).getRank());
        vo.setDepAvgRankScore(depAvgRank.size()==0?null:depAvgRank.get(0).getNumber());

        List<ScoreRankVO> depCountRank =scoreMapper.countRank(tId,user.getDepartmentId(),null,null,null);
        vo.setDepCountRank(depCountRank.size()==0?null:depCountRank.get(0).getRank());
        vo.setDepCountRankScore(depCountRank.size()==0?null:depCountRank.get(0).getNumber());

        List<ScoreRankVO> stuDepSumRank =scoreMapper.sumRank(tId,user.getDepartmentId(),1,null,null);
        vo.setStuDepSumRank(stuDepSumRank.size()==0?null:stuDepSumRank.get(0).getRank());
        vo.setStuDepSumRankScore(stuDepSumRank.size()==0?null:stuDepSumRank.get(0).getNumber());

        List<ScoreRankVO> stuDepAvgRank =scoreMapper.avgRank(tId,user.getDepartmentId(),1,null,null);
        vo.setStuDepAvgRank(stuDepAvgRank.size()==0?null:stuDepAvgRank.get(0).getRank());
        vo.setStuDepAvgRankScore(stuDepAvgRank.size()==0?null:stuDepAvgRank.get(0).getNumber());

        List<ScoreRankVO> stuDepCountRank =scoreMapper.countRank(tId,user.getDepartmentId(),1,null,null);
        vo.setStuDepCountRank(stuDepCountRank.size()==0?null:stuDepCountRank.get(0).getRank());
        vo.setStuDepCountRankScore(stuDepCountRank.size()==0?null:stuDepCountRank.get(0).getNumber());

        List<ScoreRankVO> teaDepSumRank =scoreMapper.sumRank(tId,user.getDepartmentId(),2,null,null);
        vo.setTeaDepSumRank(teaDepSumRank.size()==0?null:teaDepSumRank.get(0).getRank());
        vo.setTeaDepSumRankScore(teaDepSumRank.size()==0?null:teaDepSumRank.get(0).getNumber());

        List<ScoreRankVO> teaDepAvgRank =scoreMapper.avgRank(tId,user.getDepartmentId(),2,null,null);
        vo.setTeaDepAvgRank(teaDepAvgRank.size()==0?null:teaDepAvgRank.get(0).getRank());
        vo.setTeaDepAvgRankScore(teaDepAvgRank.size()==0?null:teaDepAvgRank.get(0).getNumber());

        List<ScoreRankVO> teaDepCountRank =scoreMapper.countRank(tId,user.getDepartmentId(),2,null,null);
        vo.setTeaDepCountRank(teaDepCountRank.size()==0?null:teaDepCountRank.get(0).getRank());
        vo.setTeaDepCountRankScore(teaDepCountRank.size()==0?null:teaDepCountRank.get(0).getNumber());

        List<ScoreRankVO> expDepSumRank =scoreMapper.sumRank(tId,user.getDepartmentId(),3,null,null);
        vo.setExpDepSumRank(expDepSumRank.size()==0?null:expDepSumRank.get(0).getRank());
        vo.setExpDepSumRankScore(expDepSumRank.size()==0?null:expDepSumRank.get(0).getNumber());

        List<ScoreRankVO> expDepAvgRank =scoreMapper.avgRank(tId,user.getDepartmentId(),3,null,null);
        vo.setExpDepAvgRank(expDepAvgRank.size()==0?null:expDepAvgRank.get(0).getRank());
        vo.setExpDepAvgRankScore(expDepAvgRank.size()==0?null:expDepAvgRank.get(0).getNumber());

        List<ScoreRankVO> expDepCountRank =scoreMapper.countRank(tId,user.getDepartmentId(),3,null,null);
        vo.setExpDepCountRank(expDepCountRank.size()==0?null:expDepCountRank.get(0).getRank());
        vo.setExpDepCountRankScore(expDepCountRank.size()==0?null:expDepCountRank.get(0).getNumber());

        List<ScoreRankVO> stuAllSumRank =scoreMapper.sumRank(tId,null,1,null,null);
        vo.setStuAllSumRank(stuAllSumRank.size()==0?null:stuAllSumRank.get(0).getRank());
        vo.setStuAllSumRankScore(stuAllSumRank.size()==0?null:stuAllSumRank.get(0).getNumber());

        List<ScoreRankVO> stuAllAvgRank =scoreMapper.avgRank(tId,null,1,null,null);
        vo.setStuAllAvgRank(stuAllAvgRank.size()==0?null:stuAllAvgRank.get(0).getRank());
        vo.setStuAllAvgRankScore(stuAllAvgRank.size()==0?null:stuAllAvgRank.get(0).getNumber());

        List<ScoreRankVO> stuAllCountRank =scoreMapper.countRank(tId,null,1,null,null);
        vo.setStuAllCountRank(stuAllCountRank.size()==0?null:stuAllCountRank.get(0).getRank());
        vo.setStuAllCountRankScore(stuAllCountRank.size()==0?null:stuAllCountRank.get(0).getNumber());

        List<ScoreRankVO> teaAllSumRank =scoreMapper.sumRank(tId,null,2,null,null);
        vo.setTeaAllSumRank(teaAllSumRank.size()==0?null:teaAllSumRank.get(0).getRank());
        vo.setTeaAllSumRankScore(teaAllSumRank.size()==0?null:teaAllSumRank.get(0).getNumber());

        List<ScoreRankVO> teaAllAvgRank =scoreMapper.avgRank(tId,null,2,null,null);
        vo.setTeaAllAvgRank(teaAllAvgRank.size()==0?null:teaAllAvgRank.get(0).getRank());
        vo.setTeaAllAvgRankScore(teaAllAvgRank.size()==0?null:teaAllAvgRank.get(0).getNumber());

        List<ScoreRankVO> teaAllCountRank =scoreMapper.countRank(tId,null,2,null,null);
        vo.setTeaAllCountRank(teaAllCountRank.size()==0?null:teaAllCountRank.get(0).getRank());
        vo.setTeaAllCountRankScore(teaAllCountRank.size()==0?null:teaAllCountRank.get(0).getNumber());

        List<ScoreRankVO> expAllSumRank =scoreMapper.sumRank(tId,null,3,null,null);
        vo.setExpAllSumRank(expAllSumRank.size()==0?null:expAllSumRank.get(0).getRank());
        vo.setExpAllSumRankScore(expAllSumRank.size()==0?null:expAllSumRank.get(0).getNumber());

        List<ScoreRankVO> expAllAvgRank =scoreMapper.avgRank(tId,null,3,null,null);
        vo.setExpAllAvgRank(expAllAvgRank.size()==0?null:expAllAvgRank.get(0).getRank());
        vo.setExpAllAvgRankScore(expAllAvgRank.size()==0?null:expAllAvgRank.get(0).getNumber());

        List<ScoreRankVO> expAllCountRank =scoreMapper.countRank(tId,null,3,null,null);
        vo.setExpAllCountRank(expAllCountRank.size()==0?null:expAllCountRank.get(0).getRank());
        vo.setExpAllCountRankScore(expAllCountRank.size()==0?null:expAllCountRank.get(0).getNumber());

        return vo;
    }

    @Override
    public ScoreAllRankVO getScoreAndNumberByRole(Integer role) {
        List<ScoreRankVO> avgRank=scoreMapper.avgRank(null,null,role,"role",null);
        List<ScoreRankVO> countRank=scoreMapper.countRank(null,null,role,"role",null);
        List<ScoreRankVO> sumRank=scoreMapper.sumRank(null,null,role,"role",null);
        ScoreAllRankVO scoreAllRankVO=new ScoreAllRankVO();
        scoreAllRankVO.setAllAvgRankScore(avgRank.size()==0?null:avgRank.get(0).getNumber());
        scoreAllRankVO.setAllCountRankScore(countRank.size()==0?null:countRank.get(0).getNumber());
        scoreAllRankVO.setAllSumRankScore(sumRank.size()==0?null:sumRank.get(0).getNumber());
        return scoreAllRankVO;
    }

    @Override
    public ScoreAllRankAndUserNameVO userNumberOne(Integer depId) {
        ScoreAllRankAndUserNameVO scoreAllRankVO=new ScoreAllRankAndUserNameVO();
        List<ScoreRankVO> avgRank=scoreMapper.avgRank(null,depId,null,null,"y");
        if(avgRank.size()>0){
            User user=userService.selectByPrimaryKey(avgRank.get(0).getT_id());
            scoreAllRankVO.setAvgName(user.getName());
            scoreAllRankVO.setAvgDName(user.getDepartmentName());
        }
        List<ScoreRankVO> countRank=scoreMapper.countRank(null,depId,null,null,"y");
        if(countRank.size()>0){
            User user=userService.selectByPrimaryKey(countRank.get(0).getT_id());
            scoreAllRankVO.setCountName(user.getName());
            scoreAllRankVO.setCountDName(user.getDepartmentName());
        }
        List<ScoreRankVO> sumRank=scoreMapper.sumRank(null,depId,null,null,"y");
        if(sumRank.size()>0){
            User user=userService.selectByPrimaryKey(sumRank.get(0).getT_id());
            scoreAllRankVO.setSumNme(user.getName());
            scoreAllRankVO.setSumDName(user.getDepartmentName());
        }
        scoreAllRankVO.setAllAvgRankScore(avgRank.size()==0?null:avgRank.get(0).getNumber());
        scoreAllRankVO.setAllCountRankScore(countRank.size()==0?null:countRank.get(0).getNumber());
        scoreAllRankVO.setAllSumRankScore(sumRank.size()==0?null:sumRank.get(0).getNumber());
        return scoreAllRankVO;
    }

    @Override
    public List<Score> getArray(Integer tId) {
        return scoreMapper.getArray(tId);
    }


    @Override
    public Map<Integer, List<Score>> selectAVGBytTIds(List<Integer> tIds) {
        if (tIds == null || tIds.size() == 0) {
            return null;
        }
        List<Score> list = scoreMapper.selectAVGBytTIds(tIds);
        Map<Integer, List<Score>> map = new HashMap<>();
        for (Integer now : tIds) {
            List<Score> scores = null;
            for (int i = 1; i <= 3; i++) {
                if (map.get(now) == null) {
                    map.put(now, scores = new ArrayList<>());
                } else {
                    scores = map.get(now);
                }
                Score score = new Score();
                score.setScore(0);
                score.setCommentatorRole(i);
                scores.add(score);
            }
        }
        for (Score now : list) {
            List<Score> scores = map.get(now.gettId());
            for (Score now2 : scores) {
                if (now.getCommentatorRole() == now2.getCommentatorRole())
                    now2.setScore(now.getScore());
            }
        }
        return map;
    }

    @Override
    public Score getMyToTeacher(Integer cId,Integer tId) {
        return scoreMapper.getMyToTeacher(cId,tId);
    }

    @Override
    public int deleteByDepId(Score score) {
        return scoreMapper.deleteByDepId(score);
    }

}
